define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'xiservice/order/index' + location.search,
                    add_url: 'xiservice/order/add',
                    edit_url: 'xiservice/order/edit',
                    del_url: 'xiservice/order/del',
                    multi_url: 'xiservice/order/multi',
                    import_url: 'xiservice/order/import',
                    table: 'xiservice_order',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        // {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'order_no', title: __('Order_no'), operate: 'LIKE',visible:false}, 
                        {field: 'service_id', title: __('订单信息'), operate:false, formatter: function (val,row) {
                              var html='';
                               html += '<div style="text-align:left">'+row.order_no+'</div>';
                               html += '<div style="text-align:left">服务名称:'+row.service.name+'</div>';
                               //html += '<div style="text-align:left">服务数量:'+row.servicenums+'</div>';
                               //html += '<div style="text-align:left">服务时长:'+row.service_duration+'分钟</div>';
                               html += '<div style="text-align:left">服务时长:无时长</div>';
                               html += '<div style="text-align:left">服务频次:'+row.fwpc+'</div>';
                               html += '<div style="text-align:left">服务时间:'+row.fwsj+'</div>';
                             return html;
                        }},    
                        {field: 'service_desc_images', title: __('危害图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},                         
                        {field: 'user.username', title: __('用户信息'), operate:false, formatter: function (val,row) {
                              var html='';
                              //html += '<div style="text-align:left">用户id:'+row.user_id+'</div>';
                              html += '<div style="text-align:left">'+row.user.username+'</div>';
                              html += '<div style="text-align:left">'+row.user.mobile+'</div>';
                              if(row.addr){
                                 html += '<div style="text-align:left">服务地址:'+row.addr+'</div>';
                             }else{
                                html += '<div style="text-align:left">服务地址:'+'-'+'</div>';
                             }
                             html += '<div style="text-align:left">详细地址:'+row.addr_detail+'</div>';
                             return html;
                        }},  
                        {field: 'worker_id', title: __('服务人员信息'), operate:false, formatter: function (val,row) {
                              var html='';
                              if(row.worker_id>0){
                                 //html += '<div style="text-align:left">服务人员id:'+row.worker_id+'</div>';
                                 html += '<div style="text-align:left">'+row.worker.contact_name+'</div>';
                                 html += '<div style="text-align:left">'+row.worker.mobile+'</div>';
                                 html += '<div style="text-align:left">服务人员介绍:'+row.worker.desc+'</div>';
                              }else{
                                 html = '<div style="text-align:left">暂无接单服务人员</div>';
                              }

                             return html;
                        }}, 
                        {field: 'tax_title', title: __('发票信息'), operate:false, formatter: function (val,row) {
                            var html='';
                          //    html += '<div style="text-align:left">上门费:'+row.visit_money+'元</div>';
                             //html += '<div style="text-align:left">发票抬头:'+row.tax_title+'</div>';
                             if(row.tax_title){
                                html += '<div style="text-align:left">发票抬头:'+row.tax_title+'</div>';
                            }else{
                                html += '<div style="text-align:left">发票抬头:用户未填写</div>';
                            }
                            if(row.tax_no){
                                html += '<div style="text-align:left">发票税号:'+row.tax_no+'</div>';
                            }else{
                                html += '<div style="text-align:left">发票税号:用户未填写</div>';
                            }
                            // html += '<div style="text-align:left">发票税号:'+row.tax_no+'</div>';
                            if(row.tax_email){
                                html += '<div style="text-align:left">接收邮箱:'+row.tax_email+'</div>';
                            }else{
                                html += '<div style="text-align:left">接收邮箱:用户未填写</div>';
                            }
                            
                           return html;
                      }},                                              
                        {field: 'user.username', title: __('用户姓名'), operate:'LIKE',visible:false}, 
                        {field: 'user.mobile', title: __('手机号码'), operate:'LIKE',visible:false},                   
                        {field: 'total_money', title: __('费用信息'), operate:false, formatter: function (val,row) {
                              var html='';
                            //    html += '<div style="text-align:left">上门费:'+row.visit_money+'元</div>';
                               html += '<div style="text-align:left">总金额:'+row.total_money+'元</div>';
                               html += '<div style="text-align:left">实付金额:'+row.pay_money+'元</div>';
                             return html;
                        }},  
                        {field: 'transaction_id', title: __('支付信息'), operate:false, formatter: function (val,row) {
                              var html='';
                               html += '<div style="text-align:left">'+row.pay_type_text+' '+row.pay_status_text+'</div>';
                               html += '<div style="text-align:left">支付状态:'+row.pay_status_text+'</div>';
                               html += '<div style="text-align:left">支付时间:'+row.pay_time_text+'</div>';
                               if(row.transaction_id){
                                 html += '<div style="text-align:left">回调信息:'+row.transaction_id+'</div>';
                               }
                         
                               
                             return html;
                        }},    
                        
                        {field: 'task_mode', title: __('订单模式'), searchList: {"0":__('议价'),"1":__('议价')}, formatter: Table.api.formatter.status},
                        
                        {field: 'order_status', title: __('Order_status'), searchList: {"10":__('Order_status 10'),"20":__('Order_status 20'),"21":__('Order_status 21'),"30":__('Order_status 30'),"40":__('Order_status 40'),"50":__('Order_status 50'),"60":__('Order_status 60'),"70":__('Order_status 70'),"0":__('Order_status 0')}, formatter: Table.api.formatter.status},
                        {field: 'check_images', title: __('到达现场图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        {field: 'construction_images', title: __('施工中的图片'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.images},
                        // {field: 'order_no', title: __('订单异常原因'), operate: 'LIKE',visible:false}, 
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,                         
                          buttons: [

                            {
                                name: 'addtabs',
                                text: __('更换师傅'),
                                title: __('更换师傅'),
                                classname: 'btn btn-xs btn-warning  btn-dialog',
                                // icon: 'fa fa-eye',
                                url: 'xiservice/order/changeworker?ids={id}',
                            },
                                // {
                                //     name: 'addtabs',
                                //     text: __('取消'),
                                //     title: __('取消'),
                                //     confirm: '确定取消订单吗？',
                                //     classname: 'btn btn-xs btn-warning  btn-ajax',
                                //     icon: 'fa fa-close',
                                //     url: 'xiservice/order/cancel?ids={id}',
                                //     refresh:true,
                                //     visible:function(row){
                                //         if(row.order_status==10||row.order_status==20||row.order_status==30){
                                //              return true;
                                //         }
                                //         return false;
                                //     }
                                // },
                                // {
                                //     name: 'addtabs',
                                //     text: __('接单'),
                                //     title: __('接单'),
                                //     confirm: '确定接单订单吗？',
                                //     classname: 'btn btn-xs btn-warning  btn-ajax',
                                //     icon: 'fa fa-caret-square-o-right',
                                //     url: 'xiservice/order/sure?ids={id}',
                                //     refresh:true,
                                //     visible:function(row){
                                //         if(row.order_status==20&&row.worker_id>0){
                                //              return true;
                                //         }
                                //         return false;
                                //     }
                                // },
                                // {
                                //     name: 'addtabs',
                                //     text: __('开始服务'),
                                //     title: __('开始服务'),
                                //     confirm: '确定开始服务吗？',
                                //     classname: 'btn btn-xs btn-warning  btn-ajax',
                                //     icon: 'fa fa-caret-square-o-right',
                                //     url: 'xiservice/order/start?ids={id}',
                                //     refresh:true,
                                //     visible:function(row){
                                //         if(row.order_status==30){
                                //              return true;
                                //         }
                                //         return false;
                                //     }
                                // },
                                // {
                                //     name: 'addtabs',
                                //     text: __('退款'),
                                //     title: __('退款'),
                                //     confirm: '确定退款吗？',
                                //     classname: 'btn btn-xs btn-warning  btn-ajax',
                                //     icon: 'fa fa-fire',
                                //     url: 'xiservice/order/refund?ids={id}',
                                //     refresh:true,
                                //     visible:function(row){
                                //         if(row.order_status==40&&row.pay_status==20&&row.is_refund==0){
                                //              return true;
                                //         }
                                //         return false;
                                //     }
                                // },
                                {
                                    name: 'addtabs',
                                    text: __('完成'),
                                    title: __('完成'),
                                    confirm: '确定已完成订单吗？',
                                    classname: 'btn btn-xs btn-warning  btn-ajax',
                                    icon: 'fa fa-check-square',
                                    url: 'xiservice/order/finish?ids={id}',
                                    refresh:true,
                                    visible:function(row){
                                        if(row.order_status==40&&row.pay_status==20&&row.is_refund==0){
                                             return true;
                                        }
                                        return false;
                                    }
                                },
                                // {
                                //     name: 'addtabs',
                                //     text: __('更换服务人员'),
                                //     title: __('更换服务人员'),
                                //     classname: 'btn btn-xs btn-warning  btn-dialog',
                                //     icon: 'fa fa-first-order',
                                //     url: 'xiservice/order/changeworker?ids={id}',
                                //     refresh:true,
                                //     visible:function(row){
                                //         if(row.task_mode==1&&(row.order_status==20||row.order_status==21)&&row.worker_id>0){
                                //              return true;
                                //         }
                                //         return false;
                                //     }
                                // },
                                // {
                                //     name: 'addtabs',
                                //     text: __('派单'),
                                //     title: __('派单'),
                                //     classname: 'btn btn-xs btn-warning  btn-dialog',
                                //     icon: 'fa fa-first-order',
                                //     url: 'xiservice/order/changeworker?ids={id}',
                                //     refresh:true,
                                //     visible:function(row){
                                //         if(row.task_mode==1&&row.order_status==20&&row.worker_id==0){
                                //              return true;
                                //         }
                                //         return false;
                                //     }
                                // },
                            //   {
                            //       name: 'addtabs',
                            //       text: __('公示'),
                            //       title: __('完成'),
                            //       confirm: '确定公示订单到任务中心吗？',
                            //       classname: 'btn btn-xs btn-warning  btn-ajax',
                            //       icon: 'fa fa-inbox',
                            //       url: 'xiservice/order/open?ids={id}',
                            //       refresh:true,
                            //       visible:function(row){
                            //           if(row.task_mode==1&&((row.order_status==20&&row.worker_id==0)||row.order_status==21)){
                            //               return true;
                            //           }
                            //           return false;
                            //       }
                            //   },
                                {
                                    name: 'addtabs',
                                    text: __('订单详情'),
                                    title: __('订单详情'),
                                    classname: 'btn btn-xs btn-warning  btn-dialog',
                                    icon: 'fa fa-eye',
                                    url: 'xiservice/order/detail?ids={id}',
                                    
                                }
                            ],
                            formatter: Table.api.formatter.buttons,
                     }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'xiservice/order/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'xiservice/order/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'xiservice/order/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        detail: function () {
            Controller.api.bindevent();
        }, 
        genhuan: function () {
            Controller.api.bindevent();
        }, 
        changeworker:function(){
           Controller.api.bindevent();
        },      
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
